package com.cc.controller;

import com.cc.entity.User;
import com.cc.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {
    //TODO 返回值为void的补充
    @RequestMapping("/re_void")
    public void reVoid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("请求test/index请求");
        //响应处理一：返回文本
        //PrintWriter out = response.getWriter();
        //{"name":"二狗子","age":12,"sex":"男","address":"马栏山"}
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json,charset=utf-8");
        response.getWriter().write("{\"name\":\"二狗子\",\"age\":12,\"sex\":\"男\",\"address\":\"马栏山\"} ");
        //out.print("<h1>success</h1>");//当成文本
        //响应处理二：重定向
//        response.sendRedirect("jsp/controller");
        //响应处理三：转发
//        request.getRequestDispatcher("request").forward(request, response);
    }

    //TODO 情况一：响应是JSON对象
    //通过spring mvc的消息转换器进行JSON响应
    @RequestMapping("/test01")
    @ResponseBody
    public User test01(){
        User user = new User();
        user.setUsername("二狗子");
        user.setAddress("马栏山");
        user.setSex("男");
        user.setBirthday(new Date());
        return user;
    }
    //TODO 情况二：响应数据是JSON对象数组
    @RequestMapping("/test02")
    @ResponseBody
    public List<User> test02(){
        User user = new User();
        user.setUsername("二狗子");
        user.setAddress("马栏山");
        user.setSex("男");
        user.setBirthday(new Date());

        User user2 = new User();
        user.setUsername("二狗子2");
        user.setAddress("马栏山2");
        user.setSex("女");
        user.setBirthday(new Date());
        return Arrays.asList(user,user2);
    }

    //TODO 情况三：请求数据是JSON 同时响应数据是JSON

    /**
     * url : http://localhost:8080/nd-springmvc01-base07/test/test03
     * 请求参数：(id=1&usernmae=二狗子&birthday=2020-2-2&address=马栏山2)
     * {
     *      "id": null,
     "username": "二狗子2",
     "birthday": 1621912501806,
     "sex": "女",
     "address": "马栏山2"
     * }
     *
     * 响应报文：
     * {
     *      "id": null,
     "username": "二狗子2",
     "birthday": 1621912501806,
     "sex": "女",
     "address": "马栏山2"
     * }
     *
     */

    @RequestMapping("/test03")
    @ResponseBody
    public List<User> test03(@RequestBody User user){
        User user2 = new User();
        user2.setUsername("二狗子2");
        user2.setAddress("马栏山2");
        user2.setSex("女");
        user2.setBirthday(new Date());
        return Arrays.asList(user,user2);
    }

    //TODO 统一异常处理
    @RequestMapping("/test04")
    public String test04(int cstId) throws CustomException{
        if(cstId==0){
            throw new CustomException("参数错误");
        }
        return "index";
    }

}
